package com.huaxin.hxmoduleworkorder.mapping;

import com.huaxin.hxmoduleworkorder.common.enums.CustomerType;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderCustomerCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderCustomerIdDTO;
import com.huaxin.hxmoduleworkorder.models.InspectionOrderCustomer;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.util.List;

@Mapper(componentModel = "spring")
public interface InspectionOrderCustomerMapping {
    @Mapping(target = "customerType", ignore = true)
    InspectionOrderCustomer toEntity(InspectionOrderCustomerCreateReqDTO reqDTO);

    @AfterMapping
    default void afterMapping(InspectionOrderCustomerCreateReqDTO in, @MappingTarget InspectionOrderCustomer entity) {
        entity.setCustomerType(CustomerType.of(in.getCustomerType()));

    }

    @Mapping(target = "customerType", ignore = true)
    void updateEntity(InspectionOrderCustomerCreateReqDTO reqDTO, @MappingTarget InspectionOrderCustomer inspectionOrderCustomer);

    @Mapping(target = "customerType", ignore = true)
    InspectionOrderCustomerIdDTO toOrderCustomerIdDTO(InspectionOrderCustomer entity);

    @AfterMapping
    default void afterMapping(InspectionOrderCustomer entity, @MappingTarget InspectionOrderCustomerIdDTO out) {
        out.setCustomerType(entity.getCustomerType().getCode());
    }

    List<InspectionOrderCustomerIdDTO> toOrderCustomerIdDTOs(List<InspectionOrderCustomer> entities);
}
